package ferri.lorenzo;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class AlbumListActivity extends ListActivity {
	
	static final String TAG = "[AlbumListActivity]";
	
	private Cursor managedCursor;
	
	OptionsMenu optionsMenu = null;
	
	String[] path = null;			//Lista delle canzoni scelta	
	String[] tit = null;			//Lista dei titoli 
	String[] alb = null;			//Lista degli album	
	String[] art = null;			//Lista degli artisti
	String[] dur = null;			//Lista delle durate delle canzoni
	long[] albart = null;			//Lista delle durate delle canzoni
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        //Recupero i dati passati al momento del lancio dell' activity
        Bundle extras = getIntent().getExtras();
        String where = new String();
        
        String query = "%mp3";
        if (extras == null) {
        	//Creo la clausola where da inserire nella query
        	Log.v(TAG, "all album list");
        	where = MediaStore.Audio.Media.IS_MUSIC + "=1" + " AND " + MediaStore.Audio.Media.DISPLAY_NAME
    				+ " LIKE \"" + query + "\"";
        }
        else {
        	//Creo la clausola where da inserire nella query
        	Log.v(TAG, "album list of specific artist");
        
        	long artistId = extras.getLong(MediaStore.Audio.Media.ARTIST_ID);
        	where = MediaStore.Audio.Media.IS_MUSIC + "=1" + 
        		" AND " + 
        		MediaStore.Audio.Media.ARTIST_ID + "=" + artistId + " AND " + MediaStore.Audio.Media.DISPLAY_NAME
				+ " LIKE \"" + query + "\"";
        	
    	
        }
        
        String sel = MediaStore.Audio.Media.ALBUM;

    	managedCursor = managedQuery(
    		MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        	new String[] {							// Audio MediaStore
	       		MediaStore.Audio.Media._ID,
	    		MediaStore.Audio.Media.ALBUM,		// Album
	    		MediaStore.Audio.Media.ALBUM_ID,	// Album_ID				
	    		MediaStore.Audio.Media.ARTIST,		// Artisti
	    		MediaStore.Audio.Media.DISPLAY_NAME
    	    },
    	    where + ") GROUP BY (" + sel,				// WHERE
    		null,										// WHERE param
    		MediaStore.Audio.Media.DEFAULT_SORT_ORDER	// ORDER BY
    	);


        ListAdapter adapter = new AlbumListAdapter(
        	this,									// context
        	R.layout.albumlist_item,				// layout
        	managedCursor,							// cursor
        	new String[] {							// from(columns)
       	    	MediaStore.Audio.Media.ALBUM,			// ALBUM
        	   	MediaStore.Audio.Media.ARTIST			// ARTISTI
           	},
        	new int[] {								// to(textids)
        		R.id.text_album, 
        		R.id.text_artist 
        	}
        );
        
        setListAdapter(adapter);
    }
    
    //Selection song event click
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
		
    	//extract the information return by the query
    	managedCursor.moveToPosition(position);
    	long albumId = managedCursor.getLong(managedCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

    	//Create Intent to send information
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.putExtra(MediaStore.Audio.Media.ALBUM_ID, albumId);
		intent.setClass(AlbumListActivity.this, SongListActivity.class);
		
		// Start activity and attend the result
		startActivityForResult(intent, 0);
    }
    
    String[] concat(String[] A, String[] B) {
 	   String[] C= new String[A.length+B.length];
 	   System.arraycopy(A, 0, C, 0, A.length);
 	   System.arraycopy(B, 0, C, A.length, B.length);

 	   return C;
 	}
	 @Override
		protected void onActivityResult(int requestCode, int resultCode,
			Intent data) {
	        // You can use the requestCode to select between multiple child
	        // activities you may have started.  Here there is only one thing
	        // we launch.
	        if (requestCode == 0) {

	            // We will be adding to our text.
	        	

	            // This is a standard resultCode that is sent back if the
	            // activity doesn't supply an explicit result.  It will also
	            // be returned if the activity failed to launch.
	            if (resultCode != RESULT_CANCELED) {
	            	if (data != null) {
		                
	        			path=data.getStringArrayExtra("path");
	        			tit = data.getStringArrayExtra("titolo");
	        			dur = data.getStringArrayExtra("durata");
	        			alb = data.getStringArrayExtra("album");
	        			art = data.getStringArrayExtra("artist");
	        			albart = data.getLongArrayExtra("art");
	                }   
	            } 
	        }       
	    }
	 
	 //Method used to insert informations about songs selected, in the intent
	 public void OK(View view) {

			switch (view.getId()) {
			case R.id.button_ok:
				
			   	Intent intent = new Intent(Intent.ACTION_VIEW);
				
				intent.putExtra("titolo", tit);
				intent.putExtra("album", alb);
				intent.putExtra("artist", art);
				intent.putExtra("durata", dur);
				intent.putExtra("path", path);
				intent.putExtra("art", albart);
				
		  
		    	setResult(RESULT_OK,intent);
		    	finish();
			}
	    }
   
}